﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using Memcached.ClientLibrary;

/// <summary>
///MemcachedTools 的摘要说明
/// </summary>
public class MemcachedTools
{

    static MemcachedTools()
    {
        String[] serverlist = System.Configuration.ConfigurationManager.AppSettings["newsMemcache"].Split(',') ;
        //String[] serverlist = { "172.28.1.200:11211", "172.28.1.201:11211" };
        SockIOPool pool = SockIOPool.GetInstance("news");
        pool.SetServers(serverlist);
        pool.Initialize();
    }

    private static MemcachedClient NewsClient
    {
        get
        {
            MemcachedClient mc = new MemcachedClient();
            mc.PoolName = "news";
            mc.EnableCompression = false;
            return mc;
        }
    }

    /// <summary>
    /// 添加一个缓存
    /// </summary>
    /// <param name="obj">缓存对象</param>
    /// <param name="sj">缓存时间</param>
    public static void Add(string key, object obj, DateTime expiry)
    {
        if (!NewsClient.Add(key, obj, expiry))
        {
            NewsClient.Set(key, obj, expiry);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static object Get(string key)
    {
        return NewsClient.Get(key);
    }
}